import java.util.Properties;

import cn.emay.common.cache.redis.RedisClient;
import cn.emay.common.cache.redis.RedisClusterClient;
import cn.emay.common.cache.redis.RedisSingleClient;
import cn.emay.common.json.JsonHelper;
import cn.emay.eucp.common.cache.CenterRedisCache;
import cn.emay.eucp.common.dto.redis.UpdateDto;

public class Test2 {

	private static RedisClient redis;

	public static void initRedis() {
		String type = "single";
		String timeout = "5000";
		String maxTotal = "16";
		String maxIdle = "8";
		String minIdle = "4";
		String maxWaitMillis = "2000";
		Properties properties = new Properties();
		properties.setProperty("timeout", timeout);
		properties.setProperty("maxIdle", maxIdle);
		properties.setProperty("maxTotal", maxTotal);
		properties.setProperty("minIdle", minIdle);
		properties.setProperty("maxWaitMillis", maxWaitMillis);
		if ("single".equalsIgnoreCase(type)) {
			String host = "100.100.10.82";
			String port = "6379";
			properties.setProperty("host", host);
			properties.setProperty("port", port);
			RedisSingleClient client1 = new RedisSingleClient();
			client1.setProperties(properties);
			client1.init();
			redis = client1;
		} else {
			String hosts = "127.0.0.1:8979,127.0.0.1:8971";
			String maxRedirections = "5";
			properties.setProperty("hosts", hosts);
			properties.setProperty("maxRedirections", maxRedirections);
			RedisClusterClient client1 = new RedisClusterClient();
			client1.setProperties(properties);
			client1.init();
			redis = client1;
		}
	}

	public static void addFirstAddress(RedisClient redisClient ,String appId, String remoteAddress) {
		redisClient.hset(CenterRedisCache.REMOTE_FIRST_ADDRESS_HASH, appId, remoteAddress, -1);
		redisClient.zadd(CenterRedisCache.REMOTE_FIRST_ADDRESS_ZSET, System.currentTimeMillis(), JsonHelper.toJsonString(UpdateDto.getAddDto(appId)));
	}

	public static void delFirstAddress(RedisClient redisClient ,String appId) {
		redisClient.hdel(CenterRedisCache.REMOTE_FIRST_ADDRESS_HASH, appId);
		redisClient.zadd(CenterRedisCache.REMOTE_FIRST_ADDRESS_ZSET, System.currentTimeMillis(), JsonHelper.toJsonString(UpdateDto.getDelDto(appId)));
	}

	public static void main(String[] args) {
		initRedis();
		String appId = "EUCP-EMY-SMS0-LHUMT";
		String remoteAddress = "100.100.10.82:24875";
		// 添加优先地址
		addFirstAddress(redis,appId, remoteAddress);
		// 删除优先地址
//		delFirstAddress(appId);
	}
}
